package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 配送货查询
 *
 * @author wu
 */

public class DeliveryListQueryFragment extends Fragment implements OnClickListener {

  private static final int REQUEST_ACQUIRE_STARTTIME = 0;
  private static final int REQUEST_ACQUIRE_FINISHTIME = 1;
  private static final int REQUEST_DATASTATUS = 2;
  private static final int REQUEST_IFPAY = 3;

  protected static final String VALUE_STATUS_ALL = "all";
  protected static final String VALUE_NEW = "new";
  protected static final String VALUE_WAITCHECK = "waitCheck";
  protected static final String VALUE_CHECKPASS = "checkPass";
  protected static final String VALUE_ABORT = "abort";
  protected static final String VALUE_TRUE = "T";
  protected static final String VALUE_FALSE = "F";
  protected static final String VALUE_ALL = "";
  public static final String EXTRA_STARTTIME = "com.isunland.managesystem.ui.EXTRA_STARTTIME";
  public static final String EXTRA_ENDTIME = "com.isunland.managesystem.ui.EXTRA_ENDTIME";
  public static final String EXTRA_DATASTATUS = "com.isunland.managesystem.ui.EXTRA_DATASTATUS";
  public static final String EXTRA_ISPAY = "com.isunland.managesystem.ui.EXTRA_ISPAY";
  public static final String REQUEST_START_TIME = "com.isunland.managesystem.ui.REQUEST_START_TIME";
  public static final String REQUEST_END_TIME = "com.isunland.managesystem.ui.REQUEST_END_TIME";
  public static final String REQUEST_DATA_STATUS = "com.isunland.managesystem.ui.EXTRA_DATASATUS";
  public static final String REQUEST_ISPAY = "com.isunland.managesystem.ui.REQUEST_ISPAY";
  TextView mStartDate_delivery;// 开始时间
  TextView mEndDate_delivery;// 结束时间
  TextView mDataState_delivery;// 数据状态
  TextView mIfPay_delivery;// 是否支付
  private String mStartTime;
  private String mEndTime;
  private String mDataStatus;// 数据
  private String mIfPay;// 是否支付

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.queryDelivery);
    mStartTime = getArguments().getString(REQUEST_START_TIME);
    mEndTime = getArguments().getString(REQUEST_END_TIME);
    mDataStatus = getArguments().getString(REQUEST_DATA_STATUS);
    mIfPay = getArguments().getString(REQUEST_ISPAY);
    LogUtil.i("mStartTime=="
        + mStartTime
        + "mEndTime=="
        + mEndTime
        + "mDataStatus=="
        + mDataStatus
        + "mIsPay=="
        + mIfPay);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_delivery_query, container, false);
    mStartDate_delivery = (TextView) view.findViewById(R.id.tv_startDate_delivery);
    mEndDate_delivery = (TextView) view.findViewById(R.id.tv_endDate_delivery);
    mDataState_delivery = (TextView) view.findViewById(R.id.tv_dataState_delivery);
    mIfPay_delivery = (TextView) view.findViewById(R.id.tv_ifPay_delivery);

    String startTime = MyDateUtil.getBeforeMonth();
    String endTime = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE);
    if (TextUtils.isEmpty(mStartTime)) {
      mStartDate_delivery.setText(startTime);
    } else {
      mStartDate_delivery.setText(mStartTime);
    }
    if (TextUtils.isEmpty(mEndTime)) {
      mEndDate_delivery.setText(endTime);
    } else {
      mEndDate_delivery.setText(mEndTime);
    }

    // 如果没有选择数据状态，默认草稿，否则显示选择的；已选择过在没有退出当前功能时再次查询显示上一次选择的结果
    if (!TextUtils.isEmpty(mDataStatus)) {
      if (VALUE_NEW.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.draft);
      } else if (VALUE_WAITCHECK.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.checking);
      } else if (VALUE_CHECKPASS.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.checkapprovve);
      } else if (VALUE_ABORT.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.stopup);
      } else if (VALUE_STATUS_ALL.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.all);
        mDataStatus = "";
      }
    } else {
      mDataState_delivery.setText(R.string.all);
      mDataStatus = "";
    }
    // 如果没有选择是否支付，默认全部，否则显示选择的；已选择过在没有退出当前功能时再次查询显示上一次选择的结果
    if (mIfPay != null) {
      if (VALUE_TRUE.equalsIgnoreCase(mIfPay)) {
        mIfPay_delivery.setText(R.string.isTrue);
      } else if (VALUE_FALSE.equalsIgnoreCase(mIfPay)) {
        mIfPay_delivery.setText(R.string.isFalse);
      } else if (VALUE_ALL.equalsIgnoreCase(mIfPay)) {
        mIfPay_delivery.setText(R.string.all);
      }
    } else {
      mIfPay_delivery.setText(R.string.all);
      mIfPay = "";
    }
    mStartDate_delivery.setOnClickListener(this);
    mEndDate_delivery.setOnClickListener(this);
    mDataState_delivery.setOnClickListener(this);
    mIfPay_delivery.setOnClickListener(this);
    return view;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 开始时间
      case R.id.tv_startDate_delivery:
        showDialog(REQUEST_ACQUIRE_STARTTIME, new Date());
        break;
      // 结束时间
      case R.id.tv_endDate_delivery:
        showDialog(REQUEST_ACQUIRE_FINISHTIME, new Date());
        break;
      // 数据状态
      case R.id.tv_dataState_delivery:
        showDialog(REQUEST_DATASTATUS, null);
        break;
      // 是否支付
      case R.id.tv_ifPay_delivery:
        showDialog(REQUEST_IFPAY, null);
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_ACQUIRE_STARTTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_ACQUIRE_FINISHTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;

      case REQUEST_DATASTATUS:
        dialog = new DeliveryDataStatusdialogFragment();
        break;
      case REQUEST_IFPAY:
        dialog = new DeliveryPayDialogFragment();
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(DeliveryListQueryFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      /**
       * 响应返回按钮
       */
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        String startTime = mStartDate_delivery.getText().toString().trim();
        String endTime = mEndDate_delivery.getText().toString().trim();
        String dataStatus = mDataState_delivery.getText().toString().trim();
        String ifPay = mIfPay_delivery.getText().toString().trim();
        String status = "";
        String pay = "";

        if (getString(R.string.draft).equalsIgnoreCase(dataStatus)) {
          status = "new";
        } else if (getString(R.string.checking).equalsIgnoreCase(dataStatus)) {
          status = "waitCheck";
        } else if (getString(R.string.checkapprovve).equalsIgnoreCase(dataStatus)) {
          status = "checkPass";
        } else if (getString(R.string.stopup).equalsIgnoreCase(dataStatus)) {
          status = "abort";
        } else if (getString(R.string.all).equalsIgnoreCase(dataStatus)) {
          status = "";
        }

        if (getString(R.string.isTrue).equalsIgnoreCase(ifPay)) {
          pay = "T";
        } else if (getString(R.string.isFalse).equalsIgnoreCase(ifPay)) {
          pay = "F";
        } else if (getString(R.string.all).equalsIgnoreCase(ifPay)) {
          pay = "";
        }
        boolean flag = MyDateUtil.startBeforeEnd(startTime, endTime);
        if (!flag) {
          Toast.makeText(getActivity(), R.string.startTimeBeforeEndTime, Toast.LENGTH_SHORT).show();
        } else {
          Intent intent = new Intent();
          intent.putExtra(DeliveryListFragment.EXTRA_STARTTIME, startTime);
          intent.putExtra(DeliveryListFragment.EXTRA_ENDTIME, endTime);
          intent.putExtra(DeliveryListFragment.EXTRA_DATASTATUS, status);
          intent.putExtra(DeliveryListFragment.EXTRA_ISPAY, pay);
          getActivity().setResult(Activity.RESULT_OK, intent);
          getActivity().finish();
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    // 开始时间
    if (requestCode == REQUEST_ACQUIRE_STARTTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mStartDate_delivery.setText(mStartTime);
    }
    // 结束时间
    if (requestCode == REQUEST_ACQUIRE_FINISHTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mEndTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      getActivity().setResult(REQUEST_ACQUIRE_FINISHTIME);
      mEndDate_delivery.setText(mEndTime);
      return;
    }
    // 数据状态
    if (requestCode == REQUEST_DATASTATUS && data != null) {
      mDataStatus = data.getStringExtra(DeliveryDataStatusdialogFragment.EXTRA_DATA_STATUS);
      LogUtil.i("mDataStatus===" + mDataStatus);
      if (VALUE_NEW.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.draft);
      } else if (VALUE_WAITCHECK.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.checking);
      } else if (VALUE_CHECKPASS.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.checkapprovve);
      } else if (VALUE_ABORT.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.stopup);
      } else if (VALUE_STATUS_ALL.equalsIgnoreCase(mDataStatus)) {
        mDataState_delivery.setText(R.string.all);
        mDataStatus = "";
      }
    }
    // 是否支付
    if (requestCode == REQUEST_IFPAY && data != null) {
      mIfPay = data.getStringExtra(DeliveryPayDialogFragment.EXTRA_IF_PAY);
      if (VALUE_TRUE.equalsIgnoreCase(mIfPay)) {
        mIfPay_delivery.setText(R.string.isTrue);
      } else if (VALUE_FALSE.equalsIgnoreCase(mIfPay)) {
        mIfPay_delivery.setText(R.string.isFalse);
      } else if (VALUE_ALL.equalsIgnoreCase(mIfPay)) {
        mIfPay_delivery.setText(R.string.all);
      }
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  public static Fragment newInstance(String start, String end, String status, String pay) {
    Bundle bundle = new Bundle();
    bundle.putString(REQUEST_START_TIME, start);
    bundle.putString(REQUEST_END_TIME, end);
    bundle.putString(REQUEST_DATA_STATUS, status);
    bundle.putString(REQUEST_ISPAY, pay);
    DeliveryListQueryFragment fragment = new DeliveryListQueryFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
